package com.zoweunion.mechanic.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zoweunion.mechanic.dao.DeptDao;
import com.zoweunion.mechanic.entity.DeptEntity;
import com.zoweunion.mechanic.service.DeptService;
import com.zoweunion.mechanic.util.MD5;
import com.zoweunion.mechanic.util.MyException;
import com.zoweunion.mechanic.util.Orgin;
import com.zoweunion.mechanic.util.UuidUtil;
import com.zoweunion.mechanic.util.constants.Constants;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestParam;

import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service("deptService")
public class DeptServiceImpl extends ServiceImpl<DeptDao, DeptEntity> implements DeptService {

    @Autowired
    private DeptDao deptDao;

    @Override
    public List<DeptEntity> queryPage(Map<String, Object> map)  {
        List<DeptEntity> departments = deptDao.queryPage(map);
        if (departments.size() > 0) {
            DeptEntity department1= new DeptEntity();
            for (int i = 0; i < departments.size() ; i++) {
                Long id = departments.get(i).getId();
                department1.setPId(id);
                Map<String, Object> hashMap = new HashMap<>();
                //todo 查询相关人员信息
                int userCount = deptDao.selectUserCount(id);
                hashMap.put("personCount", userCount);
                hashMap.put("pid", id);
                List<DeptEntity> childList=queryPage(hashMap);
                departments.get(i).setChildList(childList);
            }
        }
        return departments;
    }

    @Override
    public List<Map<String, Object>> serverList(Map<String, Object> params) {
        List<Map<String, Object>> mapList = deptDao.serverListGroupPro(params);
        mapList.forEach(item->{
            String sid = params.get("s_id").toString();
            int province_code = Orgin.getIntValue(item, "province_code");
            item.put("serverList",deptDao.selectByPro(sid,province_code));
        });
        return mapList;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUser(DeptEntity params) throws MyException {
        int i = deptDao.selectByUser(params.getMobilePhone());
        if (i > 0) {
            throw new MyException(Constants.CODE_HANDEL_FAILED,"该手机号已存在 请重新输入");
        }
        String uuid = UuidUtil.get32UUID();
        params.setUserId(uuid);
        String user_password = MD5.md5("123456");
        params.setUserPassword(user_password);
        deptDao.insertUser(params);
        //插入关系表
        deptDao.insertRelation(params);
    }

    @Override
    public void insertRelation(DeptEntity params) {
        //插入关系表
        deptDao.insertRelation(params);
    }

    @Override
    public int updateUserRelation(DeptEntity params) {
        return deptDao.updateUserRelation(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updateUser(DeptEntity params) {
        deptDao.updateUser(params);

        //更新关系表
        deptDao.updateUserRelation(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteUser(DeptEntity params) {
        deptDao.updateUserDelete(params);
        deptDao.updateUserRelationDelete(params);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteDept(Map<String, Object> params) {
        List<DeptEntity> departments = deptDao.queryPage(params);
        if (departments.size() > 0) {
            DeptEntity department1=new DeptEntity();
            for (int i = 0; i <departments.size() ; i++) {
                Long id = departments.get(i).getId();
                department1.setPId(id);
                Map<String, Object> hashMap = new HashMap<>();
                deptDao.updateDelete(id);
                deleteDept(hashMap);
            }
        }
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void deleteParent(long id) {
        deptDao.updateDelete(id);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void insertUpdateServer(DeptEntity params) {
        String userId = params.getUserId();
        deptDao.deleteUserServer(userId);
        params.getIds().forEach(item-> {
            deptDao.insertUserServer(UuidUtil.get32UUID(), userId, item);
        });
    }

    @Override
    public List<Map<String, Object>> selectServer(@RequestParam Map<String, Object>  params) {
        String userId = Orgin.getString(params, "userId");
        List<Map<String, Object>> mapList = deptDao.selectServer(userId);
        mapList.forEach(item -> {
            int province_code = Orgin.getIntValue(item, "province_code");
            item.put("serverList",deptDao.selectByServer(userId,province_code));
        });
        return mapList;
    }

    @Override
    public PageInfo<Map<String, Object>> userList(Map<String, Object> params) {
        Integer pageSize = Integer.valueOf(params.get("pageSize").toString());
        Integer currentPage = Integer.valueOf(params.get("currentPage").toString());
        PageHelper.startPage(currentPage, pageSize);
        List<Map<String, Object>> deptEntities = deptDao.userList(params);
        PageInfo<Map<String, Object>> mapPageInfo = new PageInfo<>(deptEntities);
        return mapPageInfo;
    }

    @Override
    public List<Map<String, Object>> getManagedServerList(Map<String, Object> params) {
        List<Map<String, Object>> managedServerList = deptDao.getManagedServerList(params);
        return managedServerList;
    }
}